package com.edu.zut.controller;


import com.edu.zut.entity.Role;
import com.edu.zut.result.ResponseResult;
import com.edu.zut.service.RoleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author 许文科
 * @since 2023-01-12
 */
@RestController
@RequestMapping("/api/role")
public class RoleController {
    @Resource
    private RoleService roleService;

    @GetMapping("/getRoles")
    @PreAuthorize("hasRole('admin')")
    public ResponseResult getRoles() {
        //获取当前用户的用户名
        String username = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return roleService.getRoles(username);
    }

    @PostMapping("/status")
    @PreAuthorize("hasRole('admin')")
    public ResponseResult changeStatus(@RequestBody Role role) {
        return roleService.updateStatus(role.getRoleId(), role.getStatus());
    }

}
